package com.situ.day4;

import org.junit.Test;

import java.util.Scanner;

public class HomeWork {
//    3、
//   3.1、编写一个收银台收款程序，if
//    定义输入----单价、数量、用户输入金额
//    定义输出----应收金额、找零
//    使用double类型变量 scanner.nextDouble();
//  3.2、当总价>=500时候打八折
//  3.3、考虑程序出现异常的情况，如：收款金额小于应收金额
//    若收款金额大于等于应收金额，则计算找零后输出
//    若收款金额小于应收金额，输出错误信息。
    @Test
    public void test1() {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入单价：");
        double unitPrice = scanner.nextDouble();
        System.out.println("请输入数量：");
        double count = scanner.nextDouble();
        System.out.println("请输入支付金额:");
        double payPrice = scanner.nextDouble();
        double totalPrice = unitPrice * count;
        if (totalPrice >= 500) {
            // totalPrice = totalPrice * 0.8;
            totalPrice *= 0.8;
        }
        if (payPrice > totalPrice) {
            System.out.println("找零：" + (payPrice - totalPrice));
        } else if (payPrice == totalPrice) {
            System.out.println("正好");
        } else {
            System.out.println("付款金额不够，需要额外支付" + (totalPrice - payPrice));
        }
    }


//    1、int[] scores={0,0,1,2,3,5,4,5,2,8,7,6,9,5,4,8,3,1,0,2,4,8,7,9,5,2,1,2,3,9};
//    求出上面数组中0-9分别出现的次数
//    双重for循环
    @Test
    public void test3() {
        int[] scores={0,0,1,2,3,5,4,5,2,8,7,6,9,5,4,8,3,1,0,2,4,8,7,9,5,2,1,2,3,9};
        for (int i = 0; i <= 9; i++) {
            int count = 0;//计数器
            for (int j = 0; j < scores.length; j++){
                if (i == scores[j]){
                    count++;
                }
            }
            System.out.println(i+"出现的次数为:"+count);
        }

    }

    @Test
    public void test4(){
        //求出下面面数组中0-9分别出现的次数
        int[] scores={0,0,1,2,3,5,4,5,2,8,7,6,9,5,4,8,3,1,0,2,4,8,7,9,5,2,1,2,3,9};
        int[] counts = new int[10];
        for (int i = 0; i <= 9; i++) {
            for (int j = 0; j < scores.length; j++) {
                if (i == scores[j] ){
                    counts[i]++;
                }
            }
        }
        for (int i = 0; i < counts.length; i++) {
            System.out.println(i+"共出现了"+counts[i]+"次");
        }
    }

    @Test
    public void test67(){
        //求出下面面数组中0-9分别出现的次数
        int[] scores={0,0,1,2,3,5,4,5,2,8,7,6,9,5,4,8,3,1,0,2,4,8,7,9,5,2,1,2,3,9};
        int[] counts = new int[10];
        for (int i = 0; i < scores.length; i++) {
            counts[scores[i]]++;
        }

        for (int i = 0; i < counts.length; i++) {
            System.out.println(i+"共出现了"+counts[i]+"次");
        }
    }

    //2、int[] scores={0,0,1,2,3,5,4,5,2,8,7,6,9,5,4,8,3,1,0,2,4,8,7,9,5,2,1,2,3,9};
    //要求求出其中的奇数个数和偶数个数。
    @Test
    public void test34() {
        int[] scores={0,0,1,2,3,5,4,5,2,8,7,6,9,5,4,8,3,1,0,2,4,8,7,9,5,2,1,2,3,9};
        int oddCount = 0;
        int evenCount = 0;
        for (int i = 0; i < scores.length; i++) {
            if (scores[i] % 2 == 0) {
                evenCount++;
            } else {
                oddCount++;
            }
        }
        System.out.println("奇数个数：" + oddCount);
        System.out.println("偶数个数：" + evenCount);
    }

//    16、题目：一个5位数，判断它是不是回文数。即12321是回文数，个位与万位相同，十位与千位相同。
//    char[] array = {'a' , 'b', 'c', 'b' , 'a'};
    @Test
    public void test45() {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入字符串：");
        String str = scanner.next();
        System.out.println(str);// "abcba"
        char[] array = str.toCharArray();
        //初始认为是回文
        boolean isHuiWen = true;
        for (int i = 0; i < array.length / 2; i++) {
            if (array[i] != array[array.length - i - 1]) {
                isHuiWen = false;
                break;
            }
        }

        if (isHuiWen){
            System.out.println(str + "是回文");
        } else {
            System.out.println(str + "不是回文");
        }
    }


    //17、输入一行字符串，分别统计出其中英文字母、空格、数字和其它字符的个数。
    @Test
    public void test56() {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入字符串：");
        String str = scanner.nextLine();
        System.out.println(str);
        char[] array = str.toCharArray();
        int letterCount = 0;
        int spaceCount = 0;
        int digitCount = 0;
        int otherCount = 0;
        for (int i = 0; i < array.length; i++) {
            if ((array[i] >= 'a' && array[i] <= 'z') || (array[i] >= 'A' && array[i] <= 'Z')) {
                letterCount++;
            } else if (array[i] == ' ') {
                spaceCount++;
            } else if (array[i] >= '0' && array[i] <= '9') {
                digitCount++;
            } else {
                otherCount++;
            }
        }

        System.out.println("字母出现次数：" + letterCount);
        System.out.println("空格出现次数：" + spaceCount);
        System.out.println("数字出现次数：" + digitCount);
        System.out.println("其他出现次数：" + otherCount);
    }

    //3、计算1-100以内所有奇数的和以及所有偶数的和，分别打印出来。
    @Test
    public void test324() {
        int evenSum = 0;
        int oddSum = 0;
        for (int i = 1; i <= 100; i++) {
            if (i % 2 == 0) {
                evenSum += i;
            } else {
                oddSum += i;
            }
        }
        System.out.println("奇数和：" + oddSum);
        System.out.println("偶数和：" + evenSum);
    }

    //4、用for循环输出1—1000之间能被5整除的数，且每行输出3个
    @Test
    public void test345() {
        int count = 0;
        for (int i = 1; i <= 1000; i++) {
            if (i % 5 == 0) {
                System.out.print(i + "\t");
                count++;
                if (count % 3 == 0) {
                    System.out.println();
                }
            }
        }
    }

    @Test
    public void test35() {
        for (int i = 1; i <= 1000; i++) {
            if (i % 5 == 0) {
                System.out.print(i + "\t");
                if (i % 15 == 0) {
                    System.out.println();
                }
            }
        }
    }

    @Test
    public void test333() {
        int count = 0;
        for (int i = 1; i <= 1000; i++) {
            if (i % 5 == 0) {
                System.out.print(i + "\t");
                count++;
                if (count == 3) {
                    System.out.println();
                    count = 0;
                }
            }
        }
    }

    //5、计算9的阶乘  9!=9*8*7*6*5*...*4*3*2*1
    @Test
    public void test454() {
        int jieCheng = 1;
        for (int i = 9; i >= 1 ; i--) {
            //jieCheng = jieCheng * i;
            jieCheng *= i;
        }
        System.out.println(jieCheng);
    }

    //6、使用循环打印100-200之间所有的素数（只能被1和自己整除的数叫素数）
    @Test
    public void test453() {
        //外层循环，遍历100-200，判断每个数是不是素数
        for (int i = 100; i <= 200; i++) {
            //初始认为是素数
            boolean isSuShu = true;
            for (int j = 2; j <= i - 1; j++) {
                if (i % j == 0) {
                    isSuShu = false;
                    break;
                }
            }
            if (isSuShu) {
                System.out.println(i + "是素数");
            }
        }
    }
}
